<?php
/**  
 * 行业资讯
 * @access public|private|protected
 * @param string xxx
 * @return void|int|string|boolean|array        comment 
 * @author      ZhangLiLi <379417738@qq.com>
 * @version	  v1.0.0 
 * @copyright  2017-1-17 下午4:02:08
*/
namespace Admin\Controller;
use Admin\Controller\AdminController;
class NewsController extends AdminController{
	public function _empty(){
		header("HTTP/1.0 404 Not Found");
		$this->display('Public:404');
	}
	/**
	 * 行业资讯列表显示
	 * @author      ZhangLiLi <379417738@qq.com>
	 * @version	  v1.0.0 
	 * @copyright  2017-1-17 下午4:02:27
	 */
	public function index(){
		$article=M('Article');
    	$where["status"]=array("NEQ","-1");
     	$count=$article->where($where)->count();
     	$Page = new \Think\Page($count,10);
     	$show = $Page->show();
		$list=$article->where($where)->order("add_time desc")->limit($Page->firstRow.','.$Page->listRows)->select();
		foreach($list as $k=>$v){
			$list[$k]['content'] = mb_substr(strip_tags($v['content']),0,90);
		}
		$this -> assign('list',$list);
    	$this->assign('page',$show);
    	$this -> display();
	}
	/**
	 * 行业资讯添加或修改页显示
	 * @author      ZhangLiLi <379417738@qq.com>
	 * @version	  v1.0.0 
	 * @copyright  2017-1-17 下午4:02:45
	 */
	public function insert(){
		$where['id']= I('id')?I('id'):null;
		//图片显示
		if(!empty($where['id'])){
			$but="修改";
			$article = M('Article') -> where($where) ->find();
			$this -> assign('article',$article);
		}else{
			$but="添加";
		}
		$this->assign("but",$but);
		$this -> display();
	}
	/**
	 * 行业资讯的添加或修改
	 * 功能1：完成xxxx
	 * 功能2：完成xxxxx
	 *
	 * @author      ZhangLiLi <379417738@qq.com>
	 * @version	  v1.0.0 
	 * @copyright  2017-1-17 下午4:05:39
	 */
	public function addNews(){
		$data['title']=I("title","");
		$data["keywords"]=I("keywords","");
		$data['describe']=I("describe","");
		$data['content']=I("content")?$_POST["content"]:null;
		$data['add_time']=time();
		$where['id']=I("id","");
		if($_FILES["Filedata"]["name"]){
			$file=$_FILES["Filedata"];
			$data['pic']=$this->upload($file);
			$url=M('Banner')->where($where)->find();
			unlink($_SERVER['DOCUMENT_ROOT'].$url['pic1']);
		}else{
			$data['pic']=!empty($_POST['pic_1'])?I('pic_1'):null;
		}
// 		if(empty($data['pic'])){
// 			$this->error("请上传图片");
// 		}
		if(!empty($where['id'])){
			//修改
			$list=M('Article')->where($where)->save($data);
			$str="修改成功";$er_str="修改失败";
		}else{
			//添加
			$data['status']=I("status","1");
			$list=M("Article")->add($data);
			$str="添加成功";$er_str="添加失败";
		}
		if($list){
			$this->success($str,U('News/index'));
		}else{
			$this->error($er_str);
		}
	}
	/**
	 * 修改状态
	 * @author      ZhangLiLi <379417738@qq.com>
	 * @version	  v1.0.0 
	 * @copyright  2017-1-17 下午4:31:43
	 */
	public function open(){
		$where['id']=I('id')?I('id'):null;
		$list=M('Article')->where($where)->find();
		if($list['status']==2){
			$list=M('Article')->where($where)->setField('status','1');
			$this->success('状态修改成功');
		}
		if($list['status']==1){
			$list=M('Article')->where($where)->setField('status','2');
			$this->success('状态修改成功');
		}
	}
	/**
	 * 删除把状态改为-1
	 * @author      ZhangLiLi <379417738@qq.com>
	 * @version	  v1.0.0 
	 * @copyright  2017-1-17 下午4:32:49
	 */
	public function del(){
		$where['id']=I("id","");
		if(!empty($where['id'])){
			$list=M('Article')->where($where)->find();
			if($list["status"]==2){
				$res=M('Article')->where($where)->setField('status','-1');
				if($res){
					$this->success('删除成功',U('News/index'));
				}else{
					$this->error('删除失败');
				}
			}else{
				$this->error("显示状态下不能删除");
			}
		}
	}
}